﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Vds2465Telegrams;

namespace Vds2465Parser
{
    internal static class GenParse_ReportType_Disruption
    {
        #region private constants

        internal const byte MsgBlockOnlyValue = 0x30;

        private const byte GeneralByte = 0x00;
        private const byte PrimaryLineByte = 0x01;
        private const byte NetworkByte = 0x02;
        private const byte BatteryByte = 0x03;
        private const byte TransmissionPathByte = 0x04;
        private const byte GroundFaultByte = 0x05;
        private const byte TestByte = 0x06;
        private const byte EnergySupplyByte = 0x07;
        private const byte BufferOverflowByte = 0x08;
        private const byte NotSentMessagesByte = 0x09;
        private const byte TransmissionPath_1Byte = 0x0A;
        private const byte TransmissionPath_2Byte = 0x0B;

        #endregion private constants

        #region internal methods

        internal static byte GenerateByte(ReportType_Disruption reportTypeDisruption)
        {
            byte resultByte = 0x0;

            // Disruption has 0x1 in Bits 6,5,4
            resultByte += MsgBlockOnlyValue;

            // add the report type sub type
            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.General)
            {
                resultByte |= GeneralByte;
                return resultByte;
            }

            // add the report type sub type
            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.PrimaryLine)
            {
                resultByte |= PrimaryLineByte;
                return resultByte;
            }

            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.Network)
            {
                resultByte |= NetworkByte;
                return resultByte;
            }

            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.Battery)
            {
                resultByte |= BatteryByte;
                return resultByte;
            }

            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.TransmissionPath)
            {
                resultByte |= TransmissionPathByte;
                return resultByte;
            }

            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.GroundFault)
            {
                resultByte |= GroundFaultByte;
                return resultByte;
            }

            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.Test)
            {
                resultByte |= TestByte;
                return resultByte;
            }

            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.EnergySupply)
            {
                resultByte |= EnergySupplyByte;
                return resultByte;
            }
            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.BufferOverflow)
            {
                resultByte |= BufferOverflowByte;
                return resultByte;
            }
            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.NotSentMessages)
            {
                resultByte |= NotSentMessagesByte;
                return resultByte;
            }
            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.TransmissionPath_1)
            {
                resultByte |= TransmissionPath_1Byte;
                return resultByte;
            }
            if (reportTypeDisruption.ReportSubType == eReportSubType_Disruption.TransmissionPath_2)
            {
                resultByte |= TransmissionPath_2Byte;
                return resultByte;
            }

            // TODO handle error (unknown eReportSubType_Disruption)
            return resultByte;
        }

        internal static bool ParseByte(byte byteValue, out eReportSubType_Disruption reportSubTypeDisruption)
        {
            // set arbitary default value
            reportSubTypeDisruption = eReportSubType_Disruption.General;

            // get only bits 6,5,4
            byte messageBlock = (byte)(byteValue & (byte)0x70);
            if (messageBlock == MsgBlockOnlyValue)
            {
                // get only bits 3,2,1,0
                byte subType = (byte)(byteValue & (byte)0x0f);
                if (subType == GeneralByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.General;
                    return true;
                }
                if (subType == PrimaryLineByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.PrimaryLine;
                    return true;
                }
                if (subType == NetworkByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.Network;
                    return true;
                }
                if (subType == BatteryByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.Battery;
                    return true;
                }
                if (subType == TransmissionPathByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.TransmissionPath;
                    return true;
                }
                if (subType == GroundFaultByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.GroundFault;
                    return true;
                }
                if (subType == TestByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.Test;
                    return true;
                }
                if (subType == EnergySupplyByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.EnergySupply;
                    return true;
                }
                if (subType == BufferOverflowByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.BufferOverflow;
                    return true;
                }
                if (subType == NotSentMessagesByte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.NotSentMessages;
                    return true;
                }
                if (subType == TransmissionPath_1Byte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.TransmissionPath_1;
                    return true;
                }
                if (subType == TransmissionPath_2Byte)
                {
                    reportSubTypeDisruption = eReportSubType_Disruption.TransmissionPath_2;
                    return true;
                }

                // bits 3,2,1,0 are wrong
                return false;
            }

            // bits 6,5,4 are wrong
            return false;
        }

        #endregion internal methods
    }
}

